import 'package:animal/utils/screen_util.dart';
import 'package:animal/widgets/image_error.dart';
import 'package:animal/widgets/image_loading.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

///通用图片widget，主要是用于网络图片使用，避免重复大量编写
class ImageWidget extends StatelessWidget {
  final String? imageUrl; //图片链接
  final BorderRadius? borderRadius; //图片圆角
  final void Function()? onTap; //图片单点事件
  final void Function()? longTap; //图片长按事件
  final double? width; //图片宽度
  final double? height; //图片高度
  final double? borderWidth; //图片边框宽度

  const ImageWidget({
    Key? key,
    this.imageUrl,
    this.borderRadius,
    this.onTap,
    this.width = 200,
    this.height = 200,
    this.longTap,
    this.borderWidth,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: onTap,
        onLongPress: longTap,
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(width: ScreenUtil().L(borderWidth??0),color: Colors.white),
            borderRadius: BorderRadius.circular(ScreenUtil().L(3))
          ),
          child: ClipRRect(
            borderRadius: borderRadius??BorderRadius.circular(ScreenUtil().L(20)),
            child: CachedNetworkImage(
              imageUrl: imageUrl??"https://cdn.pixabay.com/photo/2016/11/22/07/09/spruce-1848543__340.jpg",
              placeholder: (context, url) => const ImageLoadingWidget(),
              errorWidget: (controller, url, err) => ImageErrorWidget(),
              fit: BoxFit.cover,
              width: ScreenUtil().L(width??200),
              height: ScreenUtil().L(height??200),
            ),
          ),
        ));
  }
}
